import { loadJs } from '@/utils'
import * as plinko from '@/views/games/gameSeed/plinko'
import * as minesGame from '@/views/games/gameSeed/mines'
import * as diceGame from '@/views/games/gameSeed/dice'
import * as limboGame from '@/views/games/gameSeed/limbo'
import * as kenoGame from '@/views/games/gameSeed/keno'
import * as wheelGame from '@/views/games/gameSeed/wheel'
import * as diamondsGame from '@/views/games/gameSeed/diamonds'

let PlinkoRiskLevel: any = null
let KenoRiskLevel: any = null
let gameExamplesPlinko: any = null
let gameExamplesMines: any = null
let gameExamplesDice: any = null
let gameExamplesLimbo: any = null
let gameExamplesKeno: any = null
let gameExamplesWheel: any = null
let gameExamplesDiamonds: any = null
// plinko结果
const plinkoResult = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	const { rows, risk, serverSeed, clientSeed, nounce, gameVerifyContent } = params
	const index = plinko.calculatePlinko(rows, serverSeed, clientSeed, nounce)
	console.log(rows, risk, index, 'result')
	const resultDiv = document.createElement('div')
	resultDiv.style.cssText = 'background-color: #131A27;'
	if (gameVerifyContent) {
		gameVerifyContent.innerHTML = ''
		gameVerifyContent?.appendChild(resultDiv)
	}
	if (gameExamplesPlinko && gameVerifyContent) {
		resultDiv.appendChild(gameExamplesPlinko.render())
		gameExamplesPlinko.addGameResult(resultDiv.clientWidth, PlinkoRiskLevel[risk], rows, index)
		setShow(false)
		setLoading(false)
	} else {
		setShow(false)
		setLoading(false)
	}
}
export const plinkoSeedVerify = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	setShow(true)
	if (gameExamplesPlinko) {
		plinkoResult({ ...params }, setShow, setLoading)
		return
	}
	loadJs('/gameLib.js').then((originalGameLib: any) => {
		PlinkoRiskLevel = originalGameLib.PlinkoRiskLevel
		gameExamplesPlinko = new originalGameLib.PlinkoGameOut({
			onGameLoadEnd: () => plinkoResult({ ...params }, setShow, setLoading)
		})
	})
}
// mines结果
const minesResult = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	const { mines, serverSeed, clientSeed, nounce, gameVerifyContent } = params
	const index = minesGame.calculateMines(mines, serverSeed, clientSeed, nounce)
	console.log(mines, index, 'result')
	const resultDiv = document.createElement('div')
	resultDiv.style.cssText = 'width: 100%; height: 330px; background-color: #131A27;'
	if (gameVerifyContent) {
		gameVerifyContent.innerHTML = ''
		gameVerifyContent?.appendChild(resultDiv)
	}
	if (gameExamplesMines && gameVerifyContent) {
		resultDiv.appendChild(gameExamplesMines.render())
		gameExamplesMines.addGameResult(resultDiv.clientWidth, index, [])
		setShow(false)
		setLoading(false)
	} else {
		setShow(false)
		setLoading(false)
	}
}
export const minesSeedVerify = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	setShow(true)
	if (gameExamplesMines) {
		minesResult({ ...params }, setShow, setLoading)
		return
	}
	loadJs('/gameLib.js').then((originalGameLib: any) => {
		gameExamplesMines = new originalGameLib.MinesGameOut({
			onGameLoadEnd: () => minesResult({ ...params }, setShow, setLoading)
		})
	})
}
// dice结果
const diceResult = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	const { serverSeed, clientSeed, nounce, gameVerifyContent } = params
	const index = diceGame.calculateDice(serverSeed, clientSeed, nounce)
	console.log(index, 'result')
	const resultDiv = document.createElement('div')
	resultDiv.style.cssText = 'width: 100%; height: 330px; background-color: #131A27;'
	if (gameVerifyContent) {
		gameVerifyContent.innerHTML = ''
		gameVerifyContent?.appendChild(resultDiv)
	}
	if (gameExamplesDice && gameVerifyContent) {
		resultDiv.appendChild(gameExamplesDice.render())
		setTimeout(() => {
			setShow(false)
			setLoading(false)
			gameExamplesDice.addGameResult(resultDiv.clientWidth, false, 30, index)
		})
	} else {
		setShow(false)
		setLoading(false)
	}
}
export const diceSeedVerify = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	setShow(true)
	if (gameExamplesDice) {
		diceResult({ ...params }, setShow, setLoading)
		return
	}
	loadJs('/gameLib.js').then((originalGameLib: any) => {
		gameExamplesDice = new originalGameLib.DiceGameOut({
			onGameLoadEnd: () => diceResult({ ...params }, setShow, setLoading)
		})
	})
}
// limbo结果
const limboResult = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	const { serverSeed, clientSeed, nounce, gameVerifyContent } = params
	const index = limboGame.calculateLimbo(serverSeed, clientSeed, nounce)
	console.log(index, 'result')
	const resultDiv = document.createElement('div')
	resultDiv.style.cssText = 'background-color: #131A27;'
	if (gameVerifyContent) {
		gameVerifyContent.innerHTML = ''
		gameVerifyContent?.appendChild(resultDiv)
	}
	if (gameExamplesLimbo && gameVerifyContent) {
		resultDiv.appendChild(gameExamplesLimbo.render())
		gameExamplesLimbo.addGameResult(resultDiv.clientWidth, index, true)
		setShow(false)
		setLoading(false)
	} else {
		setShow(false)
		setLoading(false)
	}
}
export const limboSeedVerify = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	setShow(true)
	if (gameExamplesLimbo) {
		limboResult({ ...params }, setShow, setLoading)
		return
	}
	loadJs('/gameLib.js').then((originalGameLib: any) => {
		gameExamplesLimbo = new originalGameLib.LimboGameOut({
			onGameLoadEnd: () => limboResult({ ...params }, setShow, setLoading)
		})
	})
}
// keno结果
const kenoResult = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	const { serverSeed, clientSeed, nounce, gameVerifyContent, riskKeno } = params
	const index = kenoGame.calculateKeno(serverSeed, clientSeed, nounce)
	console.log(index, 'result')
	const resultDiv = document.createElement('div')
	resultDiv.style.cssText = 'width: 100%; height: 240px; background-color: #131A27;'
	if (gameVerifyContent) {
		gameVerifyContent.innerHTML = ''
		gameVerifyContent?.appendChild(resultDiv)
	}
	if (gameExamplesKeno && gameVerifyContent) {
		resultDiv.appendChild(gameExamplesKeno.render())
		setTimeout(() => {
			setShow(false)
			setLoading(false)
			gameExamplesKeno.addGameResult(resultDiv.clientWidth, KenoRiskLevel[riskKeno], index)
		})
	} else {
		setShow(false)
		setLoading(false)
	}
}
export const kenoSeedVerify = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	setShow(true)
	if (gameExamplesKeno) {
		kenoResult({ ...params }, setShow, setLoading)
		return
	}
	loadJs('/gameLib.js').then((originalGameLib: any) => {
		KenoRiskLevel = originalGameLib.KenoRiskLevel
		gameExamplesKeno = new originalGameLib.KenoGameOut({
			onGameLoadEnd: () => kenoResult({ ...params }, setShow, setLoading)
		})
	})
}
// wheel结果
const wheelResult = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	const { serverSeed, clientSeed, nounce, gameVerifyContent, risk, rows } = params
	const index = wheelGame.calculateWheel(serverSeed, clientSeed, nounce, rows)
	console.log(index, 'result')
	const resultDiv = document.createElement('div')
	resultDiv.style.cssText = 'width: 300px; height: 350px; background-color: #131A27;'
	if (gameVerifyContent) {
		gameVerifyContent.innerHTML = ''
		gameVerifyContent?.appendChild(resultDiv)
	}
	if (gameExamplesWheel && gameVerifyContent) {
		resultDiv.appendChild(gameExamplesWheel.render())
		setTimeout(() => {
			setShow(false)
			setLoading(false)
			gameExamplesWheel.addGameResult(resultDiv.clientWidth, risk, rows, index)
		})
	} else {
		setShow(false)
		setLoading(false)
	}
}
export const wheelSeedVerify = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	setShow(true)
	if (gameExamplesWheel) {
		wheelResult({ ...params }, setShow, setLoading)
		return
	}
	loadJs('/gameLib.js').then((originalGameLib: any) => {
		gameExamplesWheel = new originalGameLib.WheelGameOut({
			onGameLoadEnd: () => wheelResult({ ...params }, setShow, setLoading)
		})
	})
}
// diamonds结果
const diamondsResult = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	const { serverSeed, clientSeed, nounce, gameVerifyContent } = params
	const index = diamondsGame.calculateDiamonds(serverSeed, clientSeed, nounce)
	console.log(index, 'resulDiamonds')
	const resultDiv = document.createElement('div')
	resultDiv.style.cssText = 'width: 100%; height: 200px;'
	if (gameVerifyContent) {
		gameVerifyContent.innerHTML = ''
		gameVerifyContent?.appendChild(resultDiv)
	}
	if (gameExamplesDiamonds && gameVerifyContent) {
		//将游戏结果添加到结果Div上
		resultDiv.appendChild(gameExamplesDiamonds.render())
		setTimeout(() => {
			setShow(false)
			setLoading(false)
			gameExamplesDiamonds.addGameNumberResult(resultDiv.clientWidth, index)
		})
	} else {
		setShow(false)
		setLoading(false)
	}
}
export const diamondsSeedVerify = (
	params: any,
	setShow: (arg0: boolean) => void,
	setLoading: (arg0: boolean) => void
) => {
	setShow(true)
	if (gameExamplesDiamonds) {
		diamondsResult({ ...params }, setShow, setLoading)
		return
	}
	loadJs('/gameLib.js').then((originalGameLib: any) => {
		gameExamplesDiamonds = new originalGameLib.DiamondsGameOut({
			backgroundColor: '#1B2538',
			onGameLoadEnd: () => diamondsResult({ ...params }, setShow, setLoading)
		})
	})
}
